package gc3d.ftp;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

/**
 * @author Pierrick
 * easy-to-use FTP method
 */
public class GridFTP extends FTPClient {
	/**
	 * Make every directories in path
	 * @author Jan
	 * @param path
	 * @throws IOException
	 * @see org.apache.commons.net.ftp.FTPClient#makeDirectory(String)
	 */
	public void mkdirs(String path) throws IOException {
		String[] subfolders = path.split("/");
		String pathDone = new String("");
		for( String item : subfolders ) {
	 		this.makeDirectory(pathDone+item);
			pathDone += item+'/';
		}
	}

	/**
	 * Copy a file on the server (from local to remote)
	 * @param local path to the local file
	 * @param remote path to the remote file
	 * @throws IOException
	 * @see java.io.FileInputStream
	 * @see org.apache.commons.net.ftp.FTPClient#storeUniqueFile(String, InputStream)
	 */
	public boolean put(String local, String remote) throws IOException {
		this.setFileType(FTP.BINARY_FILE_TYPE);
		FileInputStream is = new FileInputStream(local);
		boolean stored = this.storeUniqueFile(remote, is);
		is.close();
		return stored;
	}

/*
	public boolean put(InputStream is, String remote) throws IOException {
		this.setFileType(FTP.BINARY_FILE_TYPE);
		return this.storeUniqueFile(remote, is);
	}
*/

	/**
	 * Copy a file localy (from remote to local)
	 * @param local path to the local file
	 * @param remote path to the remote file
	 * @return retrieveFile return
	 * @throws IOException
	 * @see java.io.FileOutputStream
	 * @see org.apache.commons.net.ftp.FTPClient#retrieveFile(String, java.io.OutputStream)
	 */
	public boolean get(String local, String remote) throws IOException {
		this.setFileType(FTP.BINARY_FILE_TYPE);
		FileOutputStream os = new FileOutputStream(local);
		boolean retrived = this.retrieveFile(remote, os);
		os.close();
		return retrived;
	}
}
